package com.carlinfo.back.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.servlet.BaseServlet;
import com.carlinfo.common.util.DateUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.pojo.ADynasty;
import com.carlinfo.demo.pojo.AKing;
import com.carlinfo.demo.service.IDemoService;
import com.carlinfo.demo.service.impl.DemoServiceImpl;

public class DemoBackServlet extends BaseServlet
{
	IDemoService demoService = new DemoServiceImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.setCharacterEncoding("UTF-8");
		this.logger.info("===========DemoBackServlet===========");
		String param = request.getParameter("method");
		if("dynastyList".equalsIgnoreCase(param))
		{
			this.dynastyList(request,response);
		}else if("dynastyInsert".equalsIgnoreCase(param))
		{
			dynastyInsert(request,response);
		}else if("dynastyInsertSubmit".equalsIgnoreCase(param))
		{
			dynastyInsertSubmit(request,response);
		}else if("dynastyUpdate".equalsIgnoreCase(param))
		{
			dynastyUpdate(request,response);
		}else if("dynastyUpdateSubmit".equalsIgnoreCase(param))
		{
			dynastyUpdateSubmit(request,response);
		}else if("kingList".equalsIgnoreCase(param))
		{
			this.kingList(request,response);
		}else if("kingInsert".equalsIgnoreCase(param))
		{
			kingInsert(request,response);
		}else if("kingInsertSubmit".equalsIgnoreCase(param))
		{
			kingInsertSubmit(request,response);
		}else if("kingUpdate".equalsIgnoreCase(param))
		{
			kingUpdate(request,response);
		}else if("kingUpdateSubmit".equalsIgnoreCase(param))
		{
			kingUpdateSubmit(request,response);
		}
	}
	private void kingUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		
	}
	private void kingUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		
	}
	private void kingInsertSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		
	}
	private void kingInsert(HttpServletRequest request, HttpServletResponse response)
	{
		
	}
	private void kingList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		HttpSession session = request.getSession();
		
		
		/* 取出查询条件 */
		String status = request.getParameter("status");
		String keyWord =request.getParameter("keyWord"); 
		String stTime = request.getParameter("stTime");
		String edTime = request.getParameter("edTime");
		
		if(keyWord == null)
		{
			keyWord = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		if(stTime == null)
		{
			stTime = "" ; 
		}
		if(edTime == null)
		{
			edTime = "" ; 
		}
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyWord", keyWord);
		condMap.put("status", status);
		condMap.put("extend", "true");
		
		Date stDate = null;
		Date edDate = null;
		if(!"".equalsIgnoreCase(stTime)&&!"".equalsIgnoreCase(edTime))
		{
			stDate = this.dateUtil.strToDateTime(stTime);
			edDate = this.dateUtil.strToDateTime(edTime);
		}
		
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 查询所有的皇上 */
		List<AKing> kingList = this.demoService.selectListKingService(pageInfoUtil, condMap);
		request.setAttribute("kingList", kingList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		request.setAttribute("currentPage", pageInfoUtil.getCurrentPage());
		request.setAttribute("pageSize", pageInfoUtil.getPageSize());
		
		request.setAttribute("keyWord", keyWord);
		request.setAttribute("status", status);
		request.setAttribute("stTime", stTime);
		request.setAttribute("edTime", edTime);
		
		String path = "/WEB-INF/jsp/back/demo/kingList.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void dynastyUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String id = request.getParameter("id");

		int age = Integer.valueOf(request.getParameter("age"));
		String capital = request.getParameter("capital");
		String context = request.getParameter("context");
		String name = request.getParameter("name");
		Date st = dateUtil.strToDateTime(request.getParameter("st"));
		Date pubTime = dateUtil.strToDateTime(request.getParameter("pubTime"));
		byte status = Byte.valueOf(request.getParameter("status"));
		
		Map<String, Object> condMap	= new HashMap<String,Object>();
		condMap.put("id", id);
		ADynasty dynasty = this.demoService.selectOneDynastyService(condMap);
		if(dynasty!=null)
		{
			dynasty.setName(name);
			dynasty.setAge(age);
			dynasty.setCapital(capital);
			dynasty.setContext(context);
			dynasty.setPubTime(pubTime);
			dynasty.setSt(st);
			dynasty.setStatus(status);
			dynasty.setUpdateTime(new Date());
			JSONObject resultJSON = this.demoService.updateOneDynastyService(dynasty);
			logger.info("成功更新:{}",resultJSON.toJSONString());
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter writer = response.getWriter();
			writer.println(resultJSON.toJSONString());
			writer.flush();
			writer.close();
		} else
		{
			logger.info("无此记录,更新失败了!");
		}
	
		
	}
	
	/**
	 * 朝代更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void dynastyUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String id = request.getParameter("id");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ADynasty dynasty = this.demoService.selectOneDynastyService(condMap);
		
		request.setAttribute("dynasty", dynasty);
		String path = "/WEB-INF/jsp/back/demo/dynastyUpdate.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void dynastyInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		/* 将页面中的值取出来,添加朝代 */
		ADynasty dynasty = new ADynasty();
		int age = Integer.valueOf(request.getParameter("age"));
		String capital = request.getParameter("capital");
		String context = request.getParameter("context");
		String name = request.getParameter("name");
		Date st = dateUtil.strToDateTime(request.getParameter("st"));
		Date pubTime = dateUtil.strToDateTime(request.getParameter("pubTime"));
		byte status = Byte.valueOf(request.getParameter("status"));
		
		dynasty.setAge(age);
		dynasty.setCapital(capital);
		dynasty.setContext(context);
		dynasty.setName(name);
		dynasty.setSt(st);
		dynasty.setStatus(status);
		dynasty.setCreateTime(new Date());
		dynasty.setPubTime(pubTime);
		dynasty.setUpdateTime(new Date());
		JSONObject jsonObj = this.demoService.saveOneDynastyService(dynasty);
		logger.info("我保存了一条:{}",jsonObj.toJSONString());
		request.setAttribute("jsonObj", jsonObj);
		/* 保存成功继续跳转到原来的界面上去 */
		this.dynastyInsert(request, response);
	}
	private void dynastyInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/back/demo/dynastyInsert.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void dynastyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		
		/* 取出查询条件 */
		String status = request.getParameter("status");
		String keyWord =request.getParameter("keyWord"); 
		String stTime = request.getParameter("stTime");
		String edTime = request.getParameter("edTime");
		
		if(keyWord == null)
		{
			keyWord = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		if(stTime == null)
		{
			stTime = "" ; 
		}
		if(edTime == null)
		{
			edTime = "" ; 
		}
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyWord", keyWord);
		condMap.put("status", status);
		
		Date stDate = null;
		Date edDate = null;
		if(!"".equalsIgnoreCase(stTime)&&!"".equalsIgnoreCase(edTime))
		{
			stDate = this.dateUtil.strToDateTime(stTime);
			edDate = this.dateUtil.strToDateTime(edTime);
		}
		
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		
		List<ADynasty> dynastyList = this.demoService.selectListDynastyService(pageInfoUtil, condMap);
		request.setAttribute("dynastyList", dynastyList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		request.setAttribute("currentPage", pageInfoUtil.getCurrentPage());
		request.setAttribute("pageSize", pageInfoUtil.getPageSize());
		
		request.setAttribute("keyWord", keyWord);
		request.setAttribute("status", status);
		request.setAttribute("stTime", stTime);
		request.setAttribute("edTime", edTime);
		
		String path = "/WEB-INF/jsp/back/demo/dynastyList.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
}
